package com.airticketbooking.controller.manager;



import com.airticketbooking.common.domain.R;
import com.airticketbooking.common.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/air/manager/uploadFile")
@Slf4j
@Api(tags = "通用接口")
public class FileUploadController {

	@Autowired
	private AliOssUtil aliOssUtil;

	//	文件上传接口
	@PostMapping("/upload")
	@ApiOperation("文件上传接口")
	public R<String> upload(MultipartFile file) {

		try {

//			原始文件名
			String originalFilename = file.getOriginalFilename();
//			截取原始文件名后缀
			String extention = originalFilename.substring(originalFilename.lastIndexOf("."));

//			新名
			String ObjectName = UUID.randomUUID().toString() + extention;


//			得到需要上传的文件流以及文件名
			String filePath = aliOssUtil.upload(file.getBytes(), ObjectName);

			return R.ok(filePath);


		} catch (IOException e) {

			log.error("文件上传失败:{}", e);
		}

		return R.ok("上传成功");
	}


}
